package cn.wzptsoft.mall.service.Impl;

import cn.wzptsoft.mall.aspect.AutoFull;
import cn.wzptsoft.mall.aspect.OperationType;
import cn.wzptsoft.mall.mapper.CategoryMapper;
import cn.wzptsoft.mall.pojo.Category;
import cn.wzptsoft.mall.pojo.vo.CategoryInfoVo;
import cn.wzptsoft.mall.pojo.vo.CategoryVo;
import cn.wzptsoft.mall.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ICategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Override
    public CategoryVo listAllCategor() {
        List<Category> list = list();
        List<CategoryInfoVo> roots = list.stream().filter(Category::getIsRoot).map(CategoryInfoVo::new).toList();
        List<CategoryInfoVo> subs = list.stream().filter(s->!s.getIsRoot()).map(CategoryInfoVo::new).toList();

        return CategoryVo.builder()
                .roots(roots)
                .subs(subs)
                .build();
    }

    @Override
    public List<CategoryInfoVo> listRootCategor() {
        List<Category> list = list();
        List<CategoryInfoVo> roots = list.stream().filter(Category::getIsRoot).map(CategoryInfoVo::new).toList();

        return roots;
    }

    @Override
    @AutoFull(OperationType.INSERT)
    public void add(Category category) {
        save(category);
    }

    @Override
    @AutoFull(OperationType.UPDATE)
    public void updateCategory(Category category) {
        updateById(category);
    }


}
